package com.adtech.adms.system.web.action;

import java.util.List;

import org.apache.log4j.Logger;

import com.adtech.adms.commons.ErrorCode;
import com.adtech.adms.commons.tree.TreeNode;
import com.adtech.adms.commons.web.action.DataGridAction;
import com.adtech.adms.system.pojo.Feature;
import com.adtech.adms.system.pojo.Role;
import com.adtech.adms.system.service.FeatureService;
import com.adtech.adms.system.service.RoleService;
import com.adtech.core.dao.query.FilterMap;
import com.adtech.core.dao.query.Pager;
import com.adtech.core.dao.query.Sort;
import com.adtech.core.exception.ServiceException;
import com.adtech.core.web.action.AjaxResult;

/**
 * 角色action
 * 角色action处理
 * @author Dormin 
 * @since 2011-8-11 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class RoleAction extends DataGridAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6045306463410230082L;

	private static final Logger log = Logger.getLogger(RoleAction.class);

	// 管理器
	private RoleService roleService;
	private Role role;
	private List<Feature> features;

	public void setFeatureService(FeatureService featureService) {
	}

	public RoleService getRoleService() {
		return roleService;
	}

	private String id;

	private List<TreeNode<Object>> roleTreeNodes;

	public List<Feature> getFeatures() {
		return features;
	}

	public void setFeatures(List<Feature> features) {
		this.features = features;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public String save() {

		try {
			roleService.save(role,this.getUser());
		} catch (ServiceException e) {
			log.error(e);
			addActionError(e.getErrorMessage());
		}

		return SUCCESS;
	}
	public String copy() {
		role = roleService.getRoleById(new Long(id));
		role.setRoleId(null);
		return SUCCESS;
	}
	public String getTree() {
		roleTreeNodes = roleService.getRoleTree();
		return SUCCESS;
	}


	public List<TreeNode<Object>>  getRoleTree() {
		return roleTreeNodes;
	}

	public String getDetail() {
		if (id != null) {
			role = roleService.getRoleById(new Long(id));
			features = roleService.getFeaturesNotRole(role);
		}
		return SUCCESS;
	}

	public String delete() {
		if (id != null && id.length() > 0) {
			try {
				roleService.delete(id,this.getUser());
				setAjaxResult(AjaxResult.success());

			} catch (ServiceException e) {
				log.error(e);
				setAjaxResult(AjaxResult.failed(e.getErrorCode(), e.getErrorMessage()));
			}
		}
		return SUCCESS;
	}

	public String getCheck() {
		if (id != null && id.length() > 0) {
			role.setRoleId(new Long(id));
		}
		if (roleService.check(role)) {
			setAjaxResult(AjaxResult.success());
		} else {
			setAjaxResult(AjaxResult.failed(ErrorCode.ROLE_DUPLICATE));
		}

		return SUCCESS;
	}

	@Override
	protected List getModelDataGridList(Pager pager, FilterMap filtermap, Sort sort) {
		return roleService.getRoleList(pager, filtermap.toExpression(), sort);

	}

}
